package com.ihr360.payroll.service.salaryreport.impl;

import com.ihr360.commons.lang.DateUtils;
import com.ihr360.payroll.dao.MergeSalaryReportTermSalaryRepository;
import com.ihr360.payroll.model.dto.salary.report.MergeSalaryReportTermSalary;
import com.ihr360.payroll.request.salaryReport.MergeSalaryQueryRequest;
import com.ihr360.payroll.service.salaryreport.MergeSalaryReportTermSalaryService;
import com.ihr360.payroll.type.ReportState;
import com.ihr360.rest.reposity.SimpleRestReposity;
import com.ihr360.rest.service.AbstractSimpleRestServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Eric on 2018/4/11/011.
 */
@Service
public class MergeSalaryReportTermSalaryServiceImpl extends AbstractSimpleRestServiceImpl<MergeSalaryReportTermSalary, Long> implements MergeSalaryReportTermSalaryService {

    @Autowired
    private MergeSalaryReportTermSalaryRepository mergeSalaryReportTermSalaryRepository;

    @Override
    public SimpleRestReposity<MergeSalaryReportTermSalary, Long> getSimpleRestReposity() {
        return mergeSalaryReportTermSalaryRepository;
    }

    @Override
    public void deleteByCompanyIdAndMergeReportId(String companyId, Long mergeReportId) {
        mergeSalaryReportTermSalaryRepository.deleteByCompanyIdAndMergeReportId(companyId, mergeReportId);
    }

    @Override
    public MergeSalaryReportTermSalary getTermSalary(String companyId, MergeSalaryQueryRequest reportRequest) {
        return this.queryTermSalary(companyId, reportRequest.getMergeReportId(), reportRequest.getTermDate(), reportRequest.getIndexno());
    }

    @Override
    public MergeSalaryReportTermSalary queryTermSalary(String companyId, Long mergeReportId, Date termDate, Integer indexno) {
        int year = Integer.parseInt(DateUtils.formatDateStr(termDate, DateUtils.STANDARD_DATE_FORMAT_Y));
        int month = Integer.parseInt(DateUtils.formatDateStr(termDate, DateUtils.STANDARD_DATE_FORMAT_M));
        return mergeSalaryReportTermSalaryRepository.findByCompanyIdAndMergeReportIdAndYearAndMonthAndIndexno(companyId, mergeReportId, year, month, indexno);
    }

    @Override
    public List<MergeSalaryReportTermSalary> queryTermSalaryByTermDateAndState(String companyId, Long mergeReportId, Date termDate, ReportState state) {
        int year = Integer.parseInt(DateUtils.formatDateStr(termDate, DateUtils.STANDARD_DATE_FORMAT_Y));
        int month = Integer.parseInt(DateUtils.formatDateStr(termDate, DateUtils.STANDARD_DATE_FORMAT_M));
        return mergeSalaryReportTermSalaryRepository.findByCompanyIdAndMergeReportIdAndYearAndMonthAndReportState(companyId, mergeReportId, year, month, state);
    }
}
